/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.OutletManagement;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import session.TX.*;
import entity.*;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Randall Chia
 */
@ManagedBean (name="outletAccessMB")
@RequestScoped
public class OutletAccessMB {
    
    @EJB
    private OutletSBRemote outletManager;
        
    
    

    /** Creates a new instance of OutletAccessMB */
    public OutletAccessMB() {
    }
    
    
    public List<Outlet> getOutletsbyManager(){
        String email;
        try{
            email = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("email").toString();
            return outletManager.retrieveOutletsByManager(email);
        }
        catch(NullPointerException e){
            return new ArrayList<Outlet>();
        }
        
    }//------------------------------------------------------------------------------------------------------
    
    
    
    //Used by an outlet manager to access an outlet
    public void outletManagerLogin(Long outletID){
        
        //Find the Outlet
        List<Outlet> outletList = outletManager.getAllOutlets();
        
        //Get the account Email and check if it is inside the manager's list.
        String email = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("email").toString();
        
        System.out.println("OutletAccessMB: " + email + " is logging into outlet: " + outletID);
        for(Outlet o: outletList){
            if(o.getId().equals(outletID)){
                
                FacesContext facesContext = FacesContext.getCurrentInstance();
                HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
                
                //Get the managers
                List<String> managers = outletManager.getOutletManagerEmailsbyOutlet(outletID);
                
                
                
                if(!managers.contains(email)){
                    return;
                }
         
                session.setAttribute("outletId", o.getId());
                session.setAttribute("outletName", o.getOutletName());
                session.setAttribute("outletOwner", o.getStakeholder().getName());
                session.setAttribute("accountId", 0);
                //session.setAttribute("accountType", "foodOutletManager");
    
            }
        } 
    }//------------------------------------------------------------------------------------------------------
    
    
    
    
        //Call this method to leave an outlet
        public void outletManagerLogout(){
            FacesContext facesContext = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
            session.removeAttribute("outletId");
            session.removeAttribute("outletName");
            session.removeAttribute("outletOwner");
        }
    
    
    
    
    
    
}
